package com.mz.mattress.admin.config;

import com.mz.mattress.api.enums.CodeTypeEnum;
import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.awt.*;

/**
 * 登陆图像验证码配置 文件读取
 */
@Data
@Component
public class LoginProperties {

    /**
     * 验证码配置
     */
    private CodeTypeEnum codeType = CodeTypeEnum.arithmetic;
    /**
     * 验证码有效期 分钟
     */
    private int expiration = 5;
    /**
     * 验证码内容长度
     */
    private int length = 2;
    /**
     * 验证码宽度
     */
    private int width = 111;
    /**
     * 验证码高度
     */
    private int height = 36;
    /**
     * 验证码字体
     */
    private String fontName;
    /**
     * 字体大小
     */
    private int fontSize = 25;


    /**
     * 获取验证码生产类
     */
    public Captcha getCaptcha() {
        return switchCaptcha(CodeTypeEnum.arithmetic);
    }

    /**
     * 依据配置信息生产验证码
     */
    private Captcha switchCaptcha(CodeTypeEnum codeType) {
        Captcha captcha;
        synchronized (this) {
            switch (codeType) {
                case arithmetic:
                    // 算术类型 https://gitee.com/whvse/EasyCaptcha
                    captcha = new ArithmeticCaptcha();
                    break;
                case chinese:
                    captcha = new ChineseCaptcha();
                    break;
                case chinese_gif:
                    captcha = new ChineseGifCaptcha();
                    break;
                case gif:
                    captcha = new GifCaptcha();
                    break;
                case spec:
                    captcha = new SpecCaptcha();
                    break;
                default:
                    captcha = new ArithmeticCaptcha();
                    break;
            }
        }
        captcha.setWidth(width);
        captcha.setHeight(height);
        // 几位数运算，默认是两位
        captcha.setLen(length);
        if (StringUtils.hasLength(fontName)) {
            captcha.setFont(new Font(fontName, Font.PLAIN, fontSize));
        }
        return captcha;
    }
}
